<?php
declare (strict_types=1);

namespace app\common\model;

class Message extends Base
{
    protected function user()
    {
        return $this->hasOne(User::class, 'id', 'uid')->field('id,code,nickname,mobile,photo');
    }

    protected function getStatusAttr($value)
    {
        switch ($value) {
            case 1:
                return '未回复';
            case 2:
                return '已回复';
            default:
                return '未查看';
        }
    }

    protected function getReplyTimeAttr($value)
    {
        return $value ? date('Y-m-d H:i:s', $value) : '未回复';
    }

    public static function add($param, $user): array
    {
        try {
            $param['uid'] = $user->id;
            parent::create($param);

        } catch (\Exception $e) {

            return ['code' => -1, 'msg' => $e->getMessage()];
        }

        return ['code' => 1, 'msg' => '提交成功，请等待平台反馈', 'url' => url('message/index')->build()];
    }
}